package com.fortune.utils;

import java.text.ParseException;

import java.util.Calendar;


public class Luozhuangpaipandayun {





	
	LuozhuangshenshaHehun myLuozhuangshenshaHehun = new LuozhuangshenshaHehun();
	luozhuanglvhehun myluozhuanglvhehun = new luozhuanglvhehun();


	public String paipan(String man, luozhuanglvhehun.sex isman) throws ParseException {

		Calendar mancal;

		try {
			mancal = myLuozhuangshenshaHehun.getCalendarfromString(man, "yyyy-MM-dd HH");

		} catch (ParseException ex) {
			return "输入不正确" + ex.getMessage();
		}

		return paipan(mancal, isman);
	}
	public String Surizs(String man) throws ParseException {
		Calendar sat;
		try {
			sat = myLuozhuangshenshaHehun.getCalendarfromString(man, "yyyy-MM-dd HH");

		} catch (ParseException ex) {
			return "输入不正确" + ex.getMessage();
		}
		
		return Rizhu(sat);
	}

	public int getyuezhuStart(String[] jiazhi, String yuezhu) {

		int start = -1;
		for (int i = 0; i < jiazhi.length; i++) {
			if (yuezhu.equals(jiazhi[i])) {
				start = i;
				break;
			}

		}

		return start;
	}
	//顺行排大运

	private String[] shundayun(String yuezhu) {

		String[] DayunStringArray = new String[8];//取八个

		String[] jiazhi = myluozhuanglvhehun.jiazhi;
		int start = getyuezhuStart(jiazhi, yuezhu);
		if (start == -1) {
			return null;
		}
		else
		{
			start++;
		}
		for (int i = 0; i < 8; i++) {
			DayunStringArray[i] = jiazhi[(start + i) % jiazhi.length];
		}

		return DayunStringArray;
	}
	//逆行排大运

	private String[] nidayun(String yuezhu) {
		String[] DayunStringArray = new String[8];//取八个

		String[] jiazhi = myluozhuanglvhehun.jiazhi;
		int start = getyuezhuStart(jiazhi, yuezhu);
		if (start == -1) {
			return null;
		}
		else
		{
			start--;
		}
		for (int i = 0; i < 8; i++) {
			DayunStringArray[i] = jiazhi[(start - i) % jiazhi.length];
		}

		return DayunStringArray;
	}


	public String[] Dayun(String nianzhu,String yuezhu, luozhuanglvhehun.sex isman) {
		String[] DayunStringArray = null;
		if (yuezhu == null || yuezhu.length() != 2) {
			return null;
		}


		if (nianzhu.startsWith("甲") || nianzhu.startsWith("丙") || nianzhu.startsWith("戊") || nianzhu.startsWith("庚") || nianzhu.startsWith("庚") || nianzhu.startsWith("壬")) {
			if (isman == luozhuanglvhehun.sex.man) {//顺行
				DayunStringArray= shundayun(yuezhu);
			} else {
				DayunStringArray=nidayun(yuezhu);
			}
		} else {
			if (isman == luozhuanglvhehun.sex.man) {
				DayunStringArray= nidayun(yuezhu);
			} else {
				DayunStringArray=shundayun(yuezhu);
			}

		}
		return DayunStringArray;
	}

	private String paipan(Calendar cal, luozhuanglvhehun.sex isman) throws ParseException {

		BaZi lunar = new BaZi(cal);
		System.out.println("此人农历的日期【" + lunar.toString() + "】");

		int time = cal.get(Calendar.HOUR_OF_DAY) / 2;
		System.out.println("此人八字【" + lunar.getYearGanZhi(time) + "】");
		System.out.println("此人的农历生肖【" + lunar.animalsYear() + "】");



		String GanZhi = lunar.getYearGanZhi(time);//取八字
		String[] tempchar = GanZhi.split(" ");

		String ganziyear = tempchar[0];//年柱
		String ganzimonth = tempchar[1];//月柱
	



		String soundyear = myLuozhuangshenshaHehun.getnumsix(ganziyear);
	

		String[] DayunArray = Dayun(ganziyear,ganzimonth, isman);
		pringst(DayunArray);
		
		return soundyear;
	}
	public String Rizhu(Calendar cal){
		BaZi lunar = new BaZi(cal);
		int time = cal.get(Calendar.HOUR_OF_DAY) / 2;
		String GanZhi = lunar.getYearGanZhi(time);//取八字
		String[] tempchar = GanZhi.split(" ");

		String ganziyear = tempchar[0];//年柱
		
		return ganziyear;
	}

	public static String pringst(String[] res) {
		String str="";
		for (int i = 0; i < res.length; i++) {
			str=str+res[i]+" ";
			System.out.print(res[i]);
			System.out.print(" ");
		}
		System.out.println("");
		return str;
	}


	public static void main(String[] args) throws ParseException {
		Luozhuangpaipandayun my=new Luozhuangpaipandayun();
		my.paipan("1991-10-23 11", luozhuanglvhehun.sex.man);
		System.out.println("年柱是："+my.Surizs("1991-10-23 11"));
	}


}
